home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / c_course.zip / CHAP11.TXT < prev    next >
Text File  |  1989-12-30  |  29KB  |  653 lines

  1.                       Chapter 11 - Structures and Unions
  2.  
  3.  
  4.                             WHAT IS A STRUCTURE?
  5.  
  6.              A structure is a user defined data type.   You have the 
  7.         ability  to  define  a new type of  data  considerably  more 
  8.         complex than the types we have been using.  A structure is a 
  9.         combination  of  several different previously  defined  data 
  10.         types,  including other structures we have defined.  An easy 
  11.         to  understand definition is,  a structure is a grouping  of 
  12.         related  data in a way convenient to the programmer or  user 
  13.         of the program.   The best way to understand a structure  is 
  14.         to  look  at  an example,  so if you will load  and  display 
  15.         STRUCT1.C, we will do just that.
  16.  
  17.              The  program begins with a structure  definition.   The 
  18.         key  word  "struct"  is followed by  some  simple  variables 
  19.         between  the  braces,   which  are  the  components  of  the 
  20.         structure.   After  the  closing brace,  you will  find  two 
  21.         variables listed,  namely "boy",  and "girl".   According to 
  22.         the  definition  of a structure,  "boy" is  now  a  variable 
  23.         composed of three elements,  "initial",  "age", and "grade".  
  24.         Each of the three fields are associated with "boy", and each 
  25.         can  store a variable of its respective type.   The variable 
  26.         "girl"  is also a variable containing three fields with  the 
  27.         same  names  as those of "boy" but  are  actually  different 
  28.         variables.  We have therefore defined 6 simple variables. 
  29.  
  30.                          A SINGLE COMPOUND VARIABLE
  31.  
  32.              Lets  examine  the  variable "boy"  more  closely.   As 
  33.         stated above, each of the three elements of "boy" are simple 
  34.         variables  and can be used anywhere in a C program  where  a 
  35.         variable of their type can be used.   For example, the "age" 
  36.         element  is  an integer variable and can therefore  be  used 
  37.         anywhere  in a C program where it is legal to use an integer 
  38.         variable,  in calculations, as a counter, in I/O operations, 
  39.         etc.   The  only problem we have is defining how to use  the 
  40.         simple  variable  "age"  which is a  part  of  the  compound 
  41.         variable  "boy".   We  use both names with a  decimal  point 
  42.         between  them with the major name first.  Thus "boy.age"  is 
  43.         the  complete  variable name for the "age" field  of  "boy".  
  44.         This  construct can be used anywhere in a C program that  it 
  45.         is desired to refer to this field.   In fact,  it is illegal 
  46.         to  use the name "boy" or "age" alone because they are  only 
  47.         partial definitions of the complete field.  Alone, the names 
  48.         refer to nothing.
  49.  
  50.                      ASSIGNING VALUES TO THE VARIABLES
  51.  
  52.              Using  the above definition,  we can assign a value  to 
  53.         each  of  the  three fields of "boy" and each of  the  three 
  54.         fields  of  "girl".   Note carefully that  "boy.initial"  is 
  55.  
  56.  
  57.                                   Page 73
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.                       Chapter 11 - Structures and Unions
  68.  
  69.  
  70.         actually  a "char" type variable,  because it  was  assigned 
  71.         that in the structure, so it must be assigned a character of 
  72.         data.   Notice  that "boy.initial" is assigned the character 
  73.         'R'  in agreement with the above rules.   The remaining  two 
  74.         fields of "boy" are assigned values in accordance with their 
  75.         respective  types.   Finally  the three fields of  girl  are 
  76.         assigned values but in a different order to illustrate  that 
  77.         the order of assignment is not critical.
  78.  
  79.                      HOW DO WE USE THE RESULTING DATA?
  80.  
  81.              Now  that  we  have assigned values to the  six  simple 
  82.         variables, we can do anything we desire with them.  In order 
  83.         to keep this first example simple,  we will simply print out 
  84.         the values to see if they really do exist as  assigned.   If 
  85.         you carefully inspect the "printf" statements,  you will see 
  86.         that there is nothing special about them.  The compound name 
  87.         of each variable is specified because that is the only valid 
  88.         name by which we can refer to these variables.
  89.  
  90.              Structures  are  a very useful method of grouping  data 
  91.         together  in  order to make a program easier  to  write  and 
  92.         understand.   This  first example is too simple to give  you 
  93.         even  a hint of the value of using structures,  but continue 
  94.         on  through  these lessons and eventually you will  see  the 
  95.         value of using structures.
  96.  
  97.              Compile and run STRUCT1.C and observe the output.
  98.  
  99.                            AN ARRAY OF STRUCTURES
  100.  
  101.              Load  and  display the next  program  named  STRUCT2.C.  
  102.         This  program  contains  the same  structure  definition  as 
  103.         before  but  this  time we define an array of  12  variables 
  104.         named "kids".   This program therefore contains 12 times 3 = 
  105.         36  simple variables,  each of which can store one  item  of 
  106.         data  provided  that  it is of the correct  type.   We  also 
  107.         define  a simple variable named "index" for use in  the  for 
  108.         loops.
  109.  
  110.              In order to assign each of the fields a value, we use a 
  111.         for loop and each pass through the loop results in assigning 
  112.         a  value to three of the fields.   One pass through the loop 
  113.         assigns all of the values for one of the "kids".  This would 
  114.         not be a very useful way to assign data in a real situation, 
  115.         but  a loop could read the data in from a file and store  it 
  116.         in  the correct fields.   You might consider this the  crude 
  117.         beginning of a data base, which it is.
  118.  
  119.              In  the next few instructions of the program we  assign 
  120.         new  values to some of the fields to illustrate  the  method 
  121.  
  122.  
  123.                                   Page 74
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.                       Chapter 11 - Structures and Unions
  134.  
  135.  
  136.         used to accomplish this.  It should be self explanatory,  so 
  137.         no additional comments will be given.
  138.  
  139.                         A NOTE TO PASCAL PROGRAMMERS
  140.  
  141.              Pascal  allows  you to copy an entire RECORD  with  one 
  142.         statement.   This is not possible in C.   You must copy each 
  143.         element  of a structure one at a time.   As improvements  to 
  144.         the   language  are  defined,   this  will  be  one  of  the 
  145.         refinements.   In fact,  some of the newer compilers already 
  146.         allow   structure   assignment.     Check   your    compiler 
  147.         documentation to see if your compiler has this feature yet.
  148.  
  149.                    WE FINALLY DISPLAY ALL OF THE RESULTS
  150.  
  151.              The last few statements contain a for loop in which all 
  152.         of  the generated values are displayed in a formatted  list.  
  153.         Compile  and  run  the program to see if it  does  what  you 
  154.         expect it to do.
  155.  
  156.                    USING POINTERS AND STRUCTURES TOGETHER
  157.  
  158.              Load  and  display  the  file named  STRUCT3.C  for  an 
  159.         example of using pointers with structures.   This program is 
  160.         identical  to the last program except that it uses  pointers 
  161.         for some of the operations.
  162.  
  163.              The  first  difference shows up in  the  definition  of 
  164.         variables  following  the  structure  definition.   In  this 
  165.         program  we define a pointer named "point" which is  defined 
  166.         as  a  pointer that points to the structure.   It  would  be 
  167.         illegal  to  try to use this pointer to point to  any  other 
  168.         variable  type.   There  is a very definite reason for  this 
  169.         restriction  in  C as we have alluded to  earlier  and  will 
  170.         review in the next few paragraphs.
  171.  
  172.              The next difference is in the for loop where we use the 
  173.         pointer  for accessing the data fields.   Since "kids" is  a 
  174.         pointer variable that points to the structure, we can define 
  175.         "point"  in  terms  of "kids".   The variable  "kids"  is  a 
  176.         constant so it cannot be changed in value,  but "point" is a 
  177.         pointer  variable  and can be assigned any value  consistent 
  178.         with  its being required to point to the structure.   If  we 
  179.         assign  the  value of "kids" to "point" then  it  should  be 
  180.         clear  that it will point to the first element of the array, 
  181.         a structure containing three fields.
  182.  
  183.                              POINTER ARITHMETIC
  184.  
  185.              Adding  1 to "point" will now cause it to point to  the 
  186.         second  field of the array because of the way  pointers  are 
  187.  
  188.  
  189.                                   Page 75
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                       Chapter 11 - Structures and Unions
  200.  
  201.  
  202.         handled in C.   The system knows that the structure contains 
  203.         three  variables  and it knows how many memory elements  are 
  204.         required to store the complete structure.   Therefore if  we 
  205.         tell it to add one to the pointer,  it will actually add the 
  206.         number  of  memory  elements  required to get  to  the  next 
  207.         element of the array.   If, for example, we were to add 4 to 
  208.         the  pointer,  it would advance the value of the  pointer  4 
  209.         times the size of the structure,  resulting in it pointing 4 
  210.         elements  farther  along the array.   This is the  reason  a 
  211.         pointer  cannot be used to point to any data type other than 
  212.         the one for which it was defined.
  213.  
  214.              Now to return to the program displayed on your monitor.  
  215.         It  should be clear from the previous discussion that as  we 
  216.         go through the loop, the pointer will point to the beginning 
  217.         of  one of the array elements each time.   We can  therefore 
  218.         use  the  pointer to reference the various elements  of  the 
  219.         structure.   Referring to the elements of a structure with a 
  220.         pointer occurs so often in C that a special method of  doing 
  221.         that  was  devised.   Using "point->initial" is the same  as 
  222.         using  "(*point).initial" which is really the way we did  it 
  223.         in the last two programs.   Remember that *point is the data 
  224.         to  which  the pointer points and the  construct  should  be 
  225.         clear.   The  "->"  is  made up of the minus  sign  and  the 
  226.         greater than sign. 
  227.  
  228.              Since the pointer points to the structure, we must once 
  229.         again  define which of the elements we wish to refer to each 
  230.         time  we use one of the elements of  the  structure.   There 
  231.         are, as we have seen, several different methods of referring 
  232.         to  the members of the structure,  and in the for loop  used 
  233.         for output at the end of the program, we use three different 
  234.         methods.   This  would  be considered very poor  programming 
  235.         practice,  but  is done this way here to illustrate  to  you 
  236.         that  they all lead to the same result.   This program  will 
  237.         probably   require   some  study  on  your  part  to   fully 
  238.         understand,  but  it will be worth your time and  effort  to 
  239.         grasp these principles.
  240.  
  241.              Compile and run this program.
  242.  
  243.                         NESTED AND NAMED STRUCTURES
  244.  
  245.              Load and display the file named NESTED.C for an example 
  246.         of  a nested structure.   The structures we have seen so far 
  247.         have been very simple,  although useful.   It is possible to 
  248.         define  structures  containing dozens and even  hundreds  or 
  249.         thousands  of  elements but it would be to  the  programmers 
  250.         advantage not to define all of the elements at one pass  but 
  251.         rather to use a hierarchical structure of definition.   This 
  252.         will be illustrated with the program on your monitor.
  253.  
  254.  
  255.                                   Page 76
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.                       Chapter 11 - Structures and Unions
  266.  
  267.  
  268.  
  269.              The  first  structure  contains three elements  but  is 
  270.         followed by no variable name.  We therefore have not defined 
  271.         any variables only a structure, but since we have included a 
  272.         name  at the beginning of the structure,  the  structure  is 
  273.         named  "person".   The name "person" can be used to refer to 
  274.         the  structure  but not to any variable  of  this  structure 
  275.         type.   It is therefore a new type that we have defined, and 
  276.         we can use the new type in nearly the same way we use "int", 
  277.         "char",  or  any  other  types that exist in  C.   The  only 
  278.         restriction is that this new name must always be  associated 
  279.         with the reserved word "struct".
  280.  
  281.              The  next  structure definition contains  three  fields 
  282.         with the middle field being the previously defined structure 
  283.         which we named "person".  The variable which has the type of 
  284.         "person" is named "descrip".   So the new structure contains 
  285.         two   simple   variables,   "grade"  and  a   string   named 
  286.         "lunch[25]",  and  the  structure  named  "descrip".   Since 
  287.         "descrip"  contains  three  variables,   the  new  structure 
  288.         actually contains 5 variables.  This structure is also given 
  289.         a name "alldat",  which is another type definition.  Finally 
  290.         we  define an array of 53 variables each with the  structure 
  291.         defined by "alldat",  and each with the name "student".   If 
  292.         that is clear,  you will see that we have defined a total of 
  293.         53 times 5 variables,  each of which is capable of storing a 
  294.         value.
  295.  
  296.                              TWO MORE VARIABLES
  297.  
  298.              Since  we  have a new type definition we can use it  to 
  299.         define  two  more variables.   The variables  "teacher"  and 
  300.         "sub"  are defined in the next statement to be variables  of 
  301.         the  type  "alldat",  so that each of  these  two  variables 
  302.         contain 5 fields which can store data.
  303.  
  304.                        NOW TO USE SOME OF THE FIELDS
  305.  
  306.              In  the next five lines of the program,  we will assign 
  307.         values to each of the fields of "teacher".   The first field 
  308.         is  the  "grade" field and is handled just  like  the  other 
  309.         structures  we  have studied because it is not part  of  the 
  310.         nested structure.  Next we wish to assign a value to her age 
  311.         which  is  part of the nested structure.   To  address  this 
  312.         field  we start with the variable name "teacher" to which we 
  313.         append  the name of the group "descrip",  and then  we  must 
  314.         define which field of the nested structure we are interested 
  315.         in,  so  we append the name "age".   The teachers status  is 
  316.         handled in exactly the same manner as her age,  but the last 
  317.         two  fields  are  assigned  strings using  the  string  copy 
  318.         "strcpy" function which must be used for string  assignment.  
  319.  
  320.  
  321.                                   Page 77
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.                       Chapter 11 - Structures and Unions
  332.  
  333.  
  334.         Notice  that the variable names in the "strcpy" function are 
  335.         still variable names even though they are made up of several 
  336.         parts each.
  337.  
  338.              The variable "sub" is assigned nonsense values in  much 
  339.         the  same  way,  but in a different order since they do  not 
  340.         have to occur in any required order.   Finally, a few of the 
  341.         "student"  variables  are assigned values  for  illustrative 
  342.         purposes  and  the program ends.   None of  the  values  are 
  343.         printed  for illustration since several were printed in  the 
  344.         last examples.
  345.  
  346.              Compile  and run this program,  but when you run it you 
  347.         may get a "stack overflow" error.   C uses it's own internal 
  348.         stack  to  store  the  automatic variables  on  but  most  C 
  349.         compilers  use only a 2048 byte stack as a  default.    This 
  350.         program  has more than that in the defined structures so  it 
  351.         will be necessary for you to increase the stack size.    The 
  352.         method  for  doing this for some compilers is given  in  the 
  353.         accompanying COMPILER.DOC file with this tutorial.   Consult 
  354.         your compiler documentation for details about your compiler.  
  355.         There  is  another way around this problem,  and that is  to 
  356.         move the structure definitions outside of the program  where 
  357.         they will be external variables and therefore static.    The 
  358.         result  is that they will not be kept on the internal  stack 
  359.         and  the  stack will therefore not overflow.    It would  be 
  360.         good for you to try both methods of fixing this problem.
  361.  
  362.                            MORE ABOUT STRUCTURES
  363.  
  364.              It is possible to continue nesting structures until you 
  365.         get  totally confused.   If you define  them  properly,  the 
  366.         computer  will  not get confused because there is no  stated 
  367.         limit as to how many levels of nesting are  allowed.   There 
  368.         is probably a practical limit of three beyond which you will 
  369.         get confused, but the language has no limit.  In addition to 
  370.         nesting, you can include as many structures as you desire in 
  371.         any level of structures,  such as defining another structure 
  372.         prior  to  "alldat" and using it in "alldat" in addition  to 
  373.         using  "person".   The  structure named  "person"  could  be 
  374.         included in "alldat" two or more times if desired,  as could 
  375.         pointers to it. 
  376.  
  377.              Structures can contain arrays of other structures which 
  378.         in  turn  can  contain  arrays  of  simple  types  or  other 
  379.         structures.   It  can go on and on until you lose all reason 
  380.         to  continue.   I am only trying to illustrate to  you  that 
  381.         structures  are  very valuable and you will find them  great 
  382.         aids to programming if you use them wisely.  Be conservative 
  383.         at first, and get bolder as you gain experience.
  384.  
  385.  
  386.  
  387.                                   Page 78
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.                       Chapter 11 - Structures and Unions
  398.  
  399.  
  400.              More  complex structures will not be illustrated  here, 
  401.         but  you will find examples of additional structures in  the 
  402.         example  programs  included  in the  last  chapter  of  this 
  403.         tutorial.     For   example,   see   the   "#include"   file 
  404.         "STRUCT.DEF". 
  405.  
  406.                               WHAT ARE UNIONS?
  407.  
  408.              Load the file named UNION1.C for an example of a union.  
  409.         Simply stated,  a union allows you a way to look at the same 
  410.         data  with  different types,  or to use the same  data  with 
  411.         different names. Examine the program on your monitor. 
  412.  
  413.              In this example we have two elements to the union,  the 
  414.         first part being the integer named "value",  which is stored 
  415.         as  a  two byte variable somewhere in the computers  memory.  
  416.         The  second  element is made up of two  character  variables 
  417.         named "first" and "second".   These two variables are stored 
  418.         in  the  same storage locations that "value" is  stored  in, 
  419.         because  that is what a union does.   A union allows you  to 
  420.         store  different types of data in the same physical  storage 
  421.         locations.  In this case, you could put an integer number in 
  422.         "value",  then retrieve it in its two halves by getting each 
  423.         half  using  the  two  names  "first"  and  "second".   This 
  424.         technique is often used to pack data bytes together when you 
  425.         are,  for  example,  combining  bytes  to  be  used  in  the 
  426.         registers of the microprocessor.  
  427.  
  428.              Accessing  the fields of the union are very similar  to 
  429.         accessing the fields of a structure and will be left to  you 
  430.         to determine by studying the example.
  431.  
  432.              One  additional  note  must  be given  here  about  the 
  433.         program.  When it is run using most compilers, the data will 
  434.         be  displayed  with two leading f's due to  the  hexadecimal 
  435.         output  promoting  the  char  type  variables  to  int   and 
  436.         extending  the  sign bit to the left.   Converting the  char 
  437.         type data fields to int type fields prior to display  should 
  438.         remove the leading f's from your display.  This will involve 
  439.         defining  two new int type variables and assigning the  char 
  440.         type  variables to them.   This will be left as an  exercise 
  441.         for  you.   Note that the same problem will come up in a few 
  442.         of the later files also. 
  443.  
  444.              Compile and run this program and observe that the  data 
  445.         is  read out as an "int" and as two "char"  variables.   The 
  446.         "char" variables are reversed in order because of the way an 
  447.         "int" variable is stored internally in your computer.  Don't 
  448.         worry  about this.  It is not a problem but it can be a very 
  449.         interesting area of study if you are so inclined.
  450.  
  451.  
  452.  
  453.                                   Page 79
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.                       Chapter 11 - Structures and Unions
  464.  
  465.  
  466.                            ANOTHER UNION EXAMPLE
  467.  
  468.              Load  and  display the file named UNION2.C for  another 
  469.         example of a union,  one which is much more common.  Suppose 
  470.         you  wished to build a large database including  information 
  471.         on many types of vehicles.  It would be silly to include the 
  472.         number of propellers on a car,  or the number of tires on  a 
  473.         boat.   In  order to keep all pertinent data,  however,  you 
  474.         would  need  those  data points for their  proper  types  of 
  475.         vehicles.   In  order to build an efficient data  base,  you 
  476.         would need several different types of data for each vehicle, 
  477.         some  of which would be common,  and some of which would  be 
  478.         different.  That is exactly what we are doing in the example 
  479.         program on your monitor.
  480.  
  481.              In this program,  we will define a complete  structure, 
  482.         then  decide which of the various types can go into it.   We 
  483.         will  start at the top and work our  way  down.   First,  we 
  484.         define  a  few constants with the #defines,  and  begin  the 
  485.         program  itself.   We define a structure named  "automobile" 
  486.         containing  several fields which you should have no  trouble 
  487.         recognizing, but we define no variables at this time.
  488.  
  489.                          A NEW CONCEPT, THE TYPEDEF
  490.  
  491.              Next  we  define a new type of data with  a  "typedef".  
  492.         This  defines  a complete new type that can be used  in  the 
  493.         same way that "int" or "char" can be used.   Notice that the 
  494.         structure  has  no name,  but at the end where  there  would 
  495.         normally be a variable name there is the name "BOATDEF".  We 
  496.         now have a new type, "BOATDEF", that can be used to define a 
  497.         structure anyplace we would like to.   Notice that this does 
  498.         not  define  any  variables,  only a  new  type  definition. 
  499.         Capitalizing  the name is a personal preference only and  is 
  500.         not  a  C standard.   It makes the "typedef" look  different 
  501.         from a variable name.
  502.  
  503.              We  finally come to the big structure that defines  our 
  504.         data using the building blocks already defined  above.   The 
  505.         structure is composed of 5 parts, two simple variables named 
  506.         "vehicle" and "weight",  followed by the union,  and finally 
  507.         the last two simple variables named "value" and "owner".  Of 
  508.         course  the union is what we need to look at carefully here, 
  509.         so focus on it for the moment.   You will notice that it  is 
  510.         composed  of four parts,  the first part being the  variable 
  511.         "car" which is a structure that we defined previously.   The 
  512.         second  part is a variable named "boat" which is a structure 
  513.         of the type "BOATDEF" previously defined.  The third part of 
  514.         the  union is the variable "airplane" which is  a  structure 
  515.         defined in place in the union.   Finally we come to the last 
  516.  
  517.  
  518.  
  519.                                   Page 80
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.                       Chapter 11 - Structures and Unions
  530.  
  531.  
  532.         part  of  the  union,  the variable named  "ship"  which  is 
  533.         another structure of the type "BOATDEF".
  534.  
  535.              I  hope  it is obvious to you that all four could  have 
  536.         been defined in any of the three ways shown,  but the  three 
  537.         different  methods  were used to show you that any could  be 
  538.         used.   In practice,  the clearest definition would probably 
  539.         have occurred by using the "typedef" for each of the parts.
  540.  
  541.                             WHAT DO WE HAVE NOW?
  542.  
  543.              We  now have a structure that can be used to store  any 
  544.         of  four different kinds of data structures.   The  size  of 
  545.         every  record will be the size of that record containing the 
  546.         largest  union.   In this case part 1 is the  largest  union 
  547.         because  it is composed of three integers,  the others being 
  548.         composed  of  an integer and a character  each.   The  first 
  549.         member  of this union would therefore determine the size  of 
  550.         all structures of this type.  The resulting structure can be 
  551.         used to store any of the four types of data, but it is up to 
  552.         the  programmer  to  keep track of what is  stored  in  each 
  553.         variable of this type.   The variable "vehicle" was designed 
  554.         into  this  structure to keep track of the type  of  vehicle 
  555.         stored here.   The four defines at the top of the page  were 
  556.         designed  to  be  used  as indicators to be  stored  in  the 
  557.         variable "vehicle". 
  558.          
  559.              A  few  examples of how to use the resulting  structure 
  560.         are given in the next few lines of the program.  Some of the 
  561.         variables are defined and a few of them are printed out  for 
  562.         illustrative purposes. 
  563.  
  564.              The union is not used too frequently,  and almost never 
  565.         by   beginning   programmers.    You   will   encounter   it 
  566.         occasionally  so  it is worth your effort to at  least  know 
  567.         what  it is.   You do not need to know the details of it  at 
  568.         this time,  so don't spend too much time studying it.   When 
  569.         you do have a need for a variant structure, a union, you can 
  570.         learn it at that time. For your own benefit, however, do not 
  571.         slight the structure. You should use the structure often.
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.         PROGRAMMING EXERCISES
  580.  
  581.         1.   Define a named structure  containing a string field for 
  582.              a name,  an integer for feet, and another for arms. Use 
  583.  
  584.  
  585.                                   Page 81
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.                       Chapter 11 - Structures and Unions
  596.  
  597.  
  598.              the new type to define an array of about 6 items.  Fill 
  599.              the fields with data and print them out as follows.
  600.  
  601.              A human being has 2 legs and 2 arms.
  602.              A dog has 4 legs and 0 arms.
  603.              A television set has 4 legs and 0 arms.
  604.              A chair has 4 legs and 2 arms.
  605.              etc.
  606.  
  607.         2.   Rewrite  exercise 1 using  a pointer to print the  data 
  608.              out.
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.  
  626.  
  627.  
  628.  
  629.  
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.                                   Page 82
  652.  
  653.